home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWFiles / Sources / SLFileSy.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  20.4 KB  |  866 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWFileSy.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef SLFILESY_H
  13. #include "SLFileSy.h"
  14. #endif
  15.  
  16. #ifndef FWFILESP_H
  17. #include "FWFileSp.h"
  18. #endif
  19.  
  20. #ifndef FWPRIDEB_H
  21. #include "FWPriDeb.h"
  22. #endif
  23.  
  24. #ifndef FWEXCDEF_H
  25. #include "FWExcDef.h"
  26. #endif
  27.  
  28. #ifndef FWSTRS_H
  29. #include "FWStrs.h"
  30. #endif
  31.  
  32. #ifndef FWBNDSTR_H
  33. #include "FWBndStr.h"
  34. #endif
  35.  
  36. #ifndef SLFILPAR_H
  37. #include "SLFilPar.h"
  38. #endif
  39.  
  40. #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
  41. #include <Script.h>
  42. #endif
  43.  
  44. #if defined(FW_BUILD_MAC) && !defined(__FILES__)
  45. #include <Files.h>
  46. #endif
  47.  
  48. #if defined(FW_BUILD_MAC) && !defined(__ERRORS__)
  49. #include <Errors.h>
  50. #endif
  51.  
  52. #if defined(FW_BUILD_WIN) && !defined(FWMEMMGR_H)
  53. #include "FWMemMgr.h"
  54. #endif
  55.  
  56. #if defined(FW_BUILD_WIN) && !defined(__DIRECT_H)
  57. #include <direct.h>
  58. #endif
  59.  
  60. #if defined(FW_BUILD_WIN) && !defined(__DOS_H)
  61. #include <dos.h>
  62. #endif
  63.  
  64. #if defined(FW_BUILD_WIN) && !defined(__IO_H)
  65. #include <io.h>
  66. #endif
  67.  
  68. #if defined(FW_BUILD_WIN) && !defined(__CTYPE_H)
  69. #include <ctype.h>
  70. #endif
  71.  
  72. #ifdef FW_BUILD_WIN16
  73. extern "C" void FAR PASCAL DOS3Call();                // We don't do "int 21h" under Windows
  74. #endif
  75.  
  76.  
  77.  
  78. //========================================================================================
  79. // Constants
  80. //========================================================================================
  81. #ifdef FW_BUILD_MAC
  82. #define FW_kMacVolumeIsSharedBitMask    0x00000200
  83. #endif
  84.  
  85.  
  86.  
  87.  
  88. //========================================================================================
  89. //    Runtime type information
  90. //========================================================================================
  91.  
  92. #ifdef FW_BUILD_MAC
  93. #pragma segment FileSystem
  94. #endif
  95.  
  96. #ifdef FW_BUILD_WIN16
  97. static FW_PlatformError privWinPrimitiveSetCurrentDir(const FW_Char* dirName, short driveNumber);
  98. static FW_PlatformError privWinPrimitiveDeleteDir(const FW_Char* dirName);
  99. static FW_PlatformError privWinPrimitiveCreateDir(const FW_Char* dirName);
  100. #endif
  101.  
  102.  
  103.  
  104.  
  105. //========================================================================================
  106. // FW_PrivFileSystem_
  107. //========================================================================================
  108.  
  109. //----------------------------------------------------------------------------------------
  110. //    FW_PrivFileSystem_CreateFile
  111. //
  112. //  Create the file specified by fileSpec.
  113. //  On the Macintosh, the file is created as a TeachText text file by default.  The user
  114. //    can always change the file type using the FW_CAccessFileInfo helper class.  
  115. //----------------------------------------------------------------------------------------
  116.  
  117. void SL_API 
  118. FW_PrivFileSystem_CreateFile(Environment* ev, 
  119.                              FW_OFileSpecification* fileSpec,
  120.                              FW_Boolean overWriteExisting)
  121. {
  122.     FW_SOM_TRY
  123.     {
  124.         FW_PlatformError theError = FW_xNoError;
  125.  
  126. #ifdef FW_BUILD_WIN
  127.         FW_CString fileName;
  128.     
  129.         fileSpec->GetFullPath(ev, fileName);
  130.     
  131.         char szFileName[_MAX_PATH];
  132.         fileName.ExportCString(szFileName);
  133. #endif
  134.     
  135. #ifdef FW_BUILD_WIN16
  136.         FW_FileAccessHandle fileHandle;
  137.         OFSTRUCT Buffer;
  138.     
  139.         if (!overWriteExisting)
  140.         {
  141.             fileHandle = ::OpenFile(szFileName, &Buffer, OF_EXIST);
  142.     
  143.             if (fileHandle != FW_kInvalidAccessHandle)
  144.                 FW_Failure(FW_xFileExists);
  145.         }
  146.         
  147.         fileHandle = ::OpenFile((LPCSTR)fileName, &Buffer, OF_CREATE);
  148.         if (fileHandle == FW_kInvalidAccessHandle)
  149.             theError = Buffer.nErrCode;
  150.         else
  151.             ::_lclose(fileHandle);
  152. #endif
  153.     
  154. #ifdef FW_BUILD_WIN32
  155.         FW_FileAccessHandle fileHandle;
  156.         long createMode = (overWriteExisting ? TRUNCATE_EXISTING | CREATE_NEW : CREATE_NEW);
  157.         
  158.         fileHandle = ::CreateFile(szFileName, 
  159.                                   GENERIC_READ | GENERIC_WRITE, 
  160.                                   0, 
  161.                                   NULL,
  162.                                   createMode, 
  163.                                   FILE_ATTRIBUTE_NORMAL, 
  164.                                   (HANDLE)NULL);
  165.                                    
  166.         if (fileHandle == FW_kInvalidAccessHandle)
  167.             theError = ::GetLastError();
  168. #endif
  169.     
  170. #ifdef FW_BUILD_MAC
  171.         short fileHandle;
  172.         FSSpec theMacSpec;
  173.         OSType aFileType;
  174.         OSType aCreatorType;
  175.     
  176.         fileSpec->MacGetFSSpec(ev, &theMacSpec);
  177.         fileSpec->MacGetTypeAndCreator(ev, &aFileType, &aCreatorType);
  178.     
  179.         theError = ::FSpCreate(&theMacSpec, aCreatorType, aFileType, smSystemScript);
  180.     
  181.         if (overWriteExisting && (theError == FW_xFileExists))
  182.         {
  183.             theError = ::FSpOpenDF(&theMacSpec, FW_kReadWrite, &fileHandle);
  184.             if (theError == FW_xNoError)
  185.             {
  186.                 // Truncate the file.  If an error occurs record it for throwing and close the file.
  187.                 theError = ::SetEOF(fileHandle, 0);
  188.                 if (theError == FW_xNoError)
  189.                     theError = ::FSClose(fileHandle);
  190.                 else
  191.                     ::FSClose(fileHandle);
  192.             }
  193.         }
  194. #endif
  195.     
  196.         FW_FailOnError(theError);
  197.     }
  198.     FW_SOM_CATCH
  199. }
  200.  
  201.  
  202. //----------------------------------------------------------------------------------------
  203. //    FW_PrivFileSystem_DeleteFile
  204. //
  205. //  Delete the file specified by fileSpec.  If the file does not exist, throw a 
  206. //    FW_XFileNotFound error containing a copy of the fileSpec.
  207. //----------------------------------------------------------------------------------------
  208.  
  209. void SL_API 
  210. FW_PrivFileSystem_DeleteFile(Environment* ev, FW_OFileSpecification* fileSpec)
  211. {
  212.     FW_SOM_TRY
  213.     {
  214.         FW_PlatformError theError = FW_xNoError;
  215.  
  216. #ifdef FW_BUILD_WIN
  217.         FW_CString fileName;
  218.     
  219.         fileSpec->GetFullPath(ev, fileName);
  220.     
  221.         char szFileName[_MAX_PATH];
  222.         fileName.ExportCString(szFileName);
  223. #endif
  224.  
  225. #ifdef FW_BUILD_WIN16
  226.         OFSTRUCT Buffer;
  227.         if (::OpenFile(szFileName, &Buffer, OF_DELETE) == FW_kInvalidAccessHandle)
  228.             theError = Buffer.nErrCode;
  229. #endif
  230.  
  231. #ifdef FW_BUILD_WIN32
  232.         if(!::DeleteFile(szFileName))
  233.             theError = ::GetLastError();
  234. #endif
  235.  
  236. #ifdef FW_BUILD_MAC
  237.         FSSpec theMacSpec;
  238.     
  239.         fileSpec->MacGetFSSpec(ev, &theMacSpec);
  240.         theError = ::FSpDelete(&theMacSpec);
  241. #endif
  242.  
  243.         FW_FailOnError(theError);
  244.     }
  245.     FW_SOM_CATCH
  246. }
  247.  
  248.  
  249. #ifdef FW_BUILD_WIN16
  250. //----------------------------------------------------------------------------------------
  251. // privWinPrimitiveCreateDir
  252. //----------------------------------------------------------------------------------------
  253. FW_PlatformError
  254. privWinPrimitiveCreateDir(const FW_Char* dirName)
  255. {
  256.     FW_PlatformError theError = FW_xNoError;
  257.     
  258.     __asm {
  259.         push     ds
  260.         lds        dx, [dirName]
  261.         mov     ah, 39h
  262.     }
  263.     
  264.     DOS3Call();
  265.         
  266.     __asm {
  267.         pop        ds
  268.         jc        _error
  269.         xor     ax, ax
  270.     }
  271.             
  272.     _error:
  273.     
  274.     __asm {
  275.         mov        theError, ax
  276.     }
  277.     
  278.     return (theError);
  279. }
  280. #endif
  281.  
  282.  
  283. //----------------------------------------------------------------------------------------
  284. // FW_PrivFileSystem_CreateDirectory
  285. //----------------------------------------------------------------------------------------
  286.  
  287. void SL_API 
  288. FW_PrivFileSystem_CreateDirectory(Environment* ev, FW_ODirectorySpecification* directory)
  289. {
  290.     FW_SOM_TRY
  291.     {
  292.         FW_PlatformError theError = FW_xNoError;
  293.     
  294. #ifdef FW_BUILD_WIN
  295.         FW_CString directoryName;
  296.     
  297.         // Get full path name and remove trailing delimiter.
  298.         //   This should always work since the directory can never be just the root path.
  299.         directory->GetFullPath(ev, directoryName);
  300.         if (directoryName.GetByteLength() > 0)
  301.             directoryName.Truncate(directoryName.GetByteLength() - 1);
  302.     
  303.         char szDirectoryName[_MAX_PATH];
  304.         directoryName.ExportCString(szDirectoryName);
  305. #endif
  306.     
  307. #ifdef FW_BUILD_WIN16
  308.         theError = privWinPrimitiveCreateDir(szDirectoryName);
  309. #endif
  310.     
  311. #ifdef FW_BUILD_WIN32
  312.         if(!::CreateDirectory(szDirectoryName, NULL))
  313.             theError = ::GetLastError();
  314. #endif
  315.     
  316. #ifdef FW_BUILD_MAC
  317.         long directoryID;
  318.         FSSpec macSpec;
  319.         
  320.         ((FW_ODirectorySpecification*)directory)->MacGetFSSpec(ev, &macSpec);
  321.         
  322.         theError = ::FSpDirCreate(&macSpec, smSystemScript, &directoryID);
  323. #endif
  324.         
  325.         FW_FailOnError(theError);
  326.     }
  327.     FW_SOM_CATCH
  328. }
  329.  
  330.  
  331. #ifdef FW_BUILD_WIN16
  332. //----------------------------------------------------------------------------------------
  333. // privWinPrimitiveDeleteDir
  334. //----------------------------------------------------------------------------------------
  335. FW_PlatformError
  336. privWinPrimitiveDeleteDir(const FW_Char* dirName)
  337. {
  338.     FW_PlatformError theError = FW_xNoError;
  339.     
  340.     __asm {
  341.         push     ds
  342.         lds        dx, [dirName]
  343.         mov     ah, 3Ah
  344.     }
  345.     
  346.     DOS3Call();
  347.         
  348.     __asm {
  349.         pop        ds
  350.         jc        _error
  351.         xor     ax, ax
  352.     }
  353.             
  354.     _error:
  355.     
  356.     __asm {
  357.         mov        theError, ax
  358.     }
  359.     
  360.     return(theError);
  361. }
  362. #endif
  363.  
  364.  
  365. //----------------------------------------------------------------------------------------
  366. // FW_PrivFileSystem_DeleteDirectory
  367. //----------------------------------------------------------------------------------------
  368.  
  369. void SL_API 
  370. FW_PrivFileSystem_DeleteDirectory(Environment* ev, FW_ODirectorySpecification* directory)
  371. {
  372.     FW_SOM_TRY
  373.     {
  374.         FW_PDirectorySpecification defaultDirectory;
  375.         
  376.         // if the directory we want to delete is the current directory, then we shouldn't
  377.         //   delete it.
  378.         if (defaultDirectory == directory)
  379.             FW_Failure(FW_xCantDeleteWorkingDirectory);
  380.             
  381.     
  382.         FW_PlatformError theError = FW_xNoError;
  383. #ifdef FW_BUILD_WIN
  384.         FW_CString directoryName;
  385.     
  386.         // Get full path name and remove trailing delimiter.
  387.         //   This should always work since the directory can never be just the root path.
  388.         directory->GetFullPath(ev, directoryName);
  389.         if (directoryName.GetByteLength() > 0)
  390.             directoryName.Truncate(directoryName.GetByteLength()-1);
  391.     
  392.         char szDirectoryName[_MAX_PATH];
  393.         directoryName.ExportCString(szDirectoryName);
  394. #endif
  395.     
  396. #ifdef FW_BUILD_WIN16
  397.         theError = privWinPrimitiveDeleteDir(szDirectoryName);            
  398. #endif
  399.     
  400. #ifdef FW_BUILD_WIN32
  401.         if(!::RemoveDirectory(szDirectoryName))
  402.             theError = ::GetLastError();
  403. #endif
  404.     
  405. #ifdef FW_BUILD_MAC
  406.         FSSpec macSpec;
  407.         ((FW_ODirectorySpecification*)directory)->MacGetFSSpec(ev, &macSpec);
  408.         
  409.         theError = ::FSpDelete(&macSpec);
  410. #endif
  411.     
  412.         FW_FailOnError(theError);
  413.     }
  414.     FW_SOM_CATCH
  415. }
  416.  
  417.  
  418. //----------------------------------------------------------------------------------------
  419. // FW_PrivFileSystem_GetCurrentDirectory
  420. //
  421. //  Returns the current working directory in directory.  
  422. //----------------------------------------------------------------------------------------
  423.  
  424. void SL_API 
  425. FW_PrivFileSystem_GetCurrentDirectory(Environment* ev, FW_ODirectorySpecification* directory)
  426. {
  427.     FW_SOM_TRY
  428.     {
  429.         FW_PlatformError theError = FW_xNoError;
  430.     
  431. #ifdef FW_BUILD_WIN
  432.         FW_CString directoryName;
  433.     
  434.         theError = FW_PrivFileSystemParser_PrivGetWorkingDirectory(directoryName);
  435.         FW_FailOnError(theError);
  436.         directory->AssignFileName(ev, directoryName);
  437. #endif
  438.     
  439.     
  440. #ifdef FW_BUILD_MAC
  441.         FSSpec directoryName;
  442.         
  443.         // Workaround for MPW Shell 3.3 bug when passing 0, 0, NULL to FSMakeFSSpec.
  444. #if defined(__MWERKS__) && GENERATING68K
  445.         Str32 nullName;
  446.         nullName[0] = 0;
  447. #else
  448.         Str32 nullName = "\p";
  449. #endif        
  450.         theError = ::FSMakeFSSpec(0, 0, nullName, &directoryName);
  451.         FW_FailOnError(theError);
  452.         directory->AssignFileSpec(ev, &directoryName);
  453. #endif
  454.     }
  455.     FW_SOM_CATCH
  456. }
  457.  
  458.  
  459. #ifdef FW_BUILD_WIN16
  460. //----------------------------------------------------------------------------------------
  461. // privWinPrimitiveSetCurrentDir
  462. //
  463. //  driveNumber should be a number from 0 to 25 where drive A=0, B=1, C=2, etc.
  464. //----------------------------------------------------------------------------------------
  465. FW_PlatformError
  466. privWinPrimitiveSetCurrentDir(const FW_Char* dirName, short driveNumber)
  467. {
  468.     FW_PlatformError theError = FW_xNoError;
  469.  
  470.     __asm {
  471.         mov        dx, driveNumber
  472.         mov     ah, 0Eh
  473.     }
  474.     
  475.     DOS3Call();
  476.         
  477.     // Now, set the directory.
  478.     __asm {
  479.         push     ds
  480.         lds        dx, [dirName]
  481.         mov     ah, 3Bh
  482.     }
  483.     
  484.     DOS3Call();
  485.         
  486.     __asm {
  487.         pop        ds
  488.         jc        _error
  489.         xor     ax, ax
  490.     }
  491.             
  492.     _error:
  493.     
  494.     __asm {
  495.         mov        theError, ax
  496.     }
  497.     
  498.     return (theError);
  499. }
  500. #endif
  501.  
  502.  
  503. //----------------------------------------------------------------------------------------
  504. // FW_PrivFileSystem_SetCurrentDirectory
  505. //
  506. // Sets the default working directory for the application.  This also sets the directory
  507. //   that appears in the standard file package when an SFP dialog is displayed.
  508. //----------------------------------------------------------------------------------------
  509.  
  510. void SL_API 
  511. FW_PrivFileSystem_SetCurrentDirectory(Environment* ev, FW_ODirectorySpecification* directory)
  512. {
  513.     FW_SOM_TRY
  514.     {
  515.         FW_PlatformError theError = FW_xNoError;
  516.         
  517.         if (!FW_PrivFileSystem_IsValidDirectory(ev, directory))
  518.             FW_Failure(FW_xInvalidDirectory);
  519.             
  520. #ifdef FW_BUILD_WIN
  521.         FW_CString directoryName;
  522.         
  523.         // Get the pathname without the trailing delimiter character.
  524.         directory->GetFullPath(ev, directoryName);
  525.         if (directoryName.GetByteLength() > 0)
  526.             directoryName.Truncate(directoryName.GetByteLength() - 1);
  527.     
  528.         char szDirectoryName[_MAX_PATH];
  529.         directoryName.ExportCString(szDirectoryName);
  530. #endif
  531.     
  532. #ifdef FW_BUILD_WIN16
  533.         // Set drive first
  534.         short driveNumber = directory.WinGetDrive() - (FW_Char)('a');
  535.         theError = privWinPrimitiveSetCurrentDir(szDirectoryName, driveNumber);
  536. #endif
  537.     
  538. #ifdef FW_BUILD_WIN32
  539.         if(!::SetCurrentDirectory(szDirectoryName))
  540.             theError = ::GetLastError();
  541. #endif
  542.     
  543. #ifdef FW_BUILD_MAC
  544.         FSSpec macSpec;
  545.     
  546.         ((FW_ODirectorySpecification*)directory)->MacGetFSSpec(ev, &macSpec);
  547.         theError =     ::HSetVol(macSpec.name, macSpec.vRefNum, macSpec.parID);
  548. #endif
  549.     
  550.         FW_FailOnError(theError);
  551.     }
  552.     FW_SOM_CATCH
  553. }
  554.  
  555.  
  556. //----------------------------------------------------------------------------------------
  557. // FW_PrivFileSystem_IsValidDirectory
  558. //
  559. // Given a fully qualified or partial pathname specified in directoryName, IsValidDirectory
  560. //   will return TRUE if the directory exists.  If the directory or drive doesn't exist, 
  561. //   IsValidDirectory will return FALSE.
  562. //----------------------------------------------------------------------------------------
  563.  
  564. FW_Boolean SL_API 
  565. FW_PrivFileSystem_IsValidDirectory(Environment* ev, FW_ODirectorySpecification* directory)
  566. {
  567.     FW_Boolean result = FALSE;
  568.  
  569.     FW_SOM_TRY
  570.     {
  571. #ifdef FW_BUILD_WIN
  572.         FW_TRY
  573.         {
  574.             FW_CString pathName;
  575.             
  576.             directory->GetFullPath(ev, pathName);
  577.             if (pathName.GetByteLength() > 0)
  578.                 pathName.Truncate(pathName.GetByteLength() - 1);
  579.                 
  580.             result = FW_PrivFileSystem_WinPathExists(ev, pathName);
  581.         }
  582.         FW_CATCH_BEGIN
  583.         FW_CATCH_NO_INSTANCE(FW_XException)
  584.         {
  585.             result = FALSE;
  586.         }
  587.         FW_CATCH_EVERYTHING()
  588.         {
  589.             FW_THROW_SAME();
  590.         }
  591.         FW_CATCH_END
  592. #endif
  593.     
  594.     
  595. #ifdef FW_BUILD_MAC
  596.         FSSpec theSpec;
  597.         OSErr theError = FW_xNoError;
  598.         
  599.         ((FW_ODirectorySpecification*)directory)->MacGetFSSpec(ev, &theSpec);
  600.         
  601.         // Try and make an FSSpec out of the directory.  If any error is returned, the
  602.         //   directory cannot exist.
  603.         theError = ::FSMakeFSSpec(theSpec.vRefNum,
  604.                                   theSpec.parID, 
  605.                                   (StringPtr)&(theSpec.name), 
  606.                                   &theSpec);
  607.         if (theError == FW_xNoError)
  608.             result = TRUE;
  609. #endif
  610.     }
  611.     FW_SOM_CATCH
  612.  
  613.     return (result);
  614. }
  615.  
  616.  
  617. //----------------------------------------------------------------------------------------
  618. // FW_PrivFileSystem_IsValidDrive
  619. //
  620. // Given a drive name specified in driveName, IsValidDrive will return TRUE if the drive
  621. //   letter is used.  For removeable media drives, there may not be anything in the drive,
  622. //   but the drive could still be valid.
  623. //----------------------------------------------------------------------------------------
  624.  
  625. FW_Boolean SL_API     
  626. FW_PrivFileSystem_IsValidDrive(Environment* ev, FW_HString driveNameRep)
  627. {
  628.     FW_Boolean result = FALSE;
  629.  
  630.     FW_SOM_TRY
  631.     {
  632.         FW_CString driveName(driveNameRep);
  633. #ifdef FW_BUILD_WIN
  634.         FW_TRY
  635.         {
  636.             if (driveName.GetByteLength() != 0)
  637.             {
  638.                 int driveType = 0;
  639.                 FW_CString32 strTemp(driveName);
  640.                 
  641.                 if (FW_PrivFileSystemParser_WinGetDrivePath(driveName, strTemp))
  642.                 {
  643.                     strTemp.ToLower();
  644.                     if ((strTemp.GetByteLength() > 1) && (strTemp[(FW_CharacterPosition)1] == FW_kDriveDelimiter))
  645.                     {
  646.     
  647. #ifdef FW_BUILD_WIN16
  648.                         FW_Char dwDriveId = strTemp[0];
  649.                         driveType = ::GetDriveType((int)dwDriveId - (int)('a'));
  650. #endif
  651.     
  652.     
  653. #ifdef FW_BUILD_WIN32
  654.                         strTemp += FW_kPathDelimiter;
  655.                         char szTemp[_MAX_PATH];
  656.                         strTemp.ExportCString(szTemp);
  657.                         driveType = ::GetDriveType(szTemp);
  658. #endif
  659.     
  660.                         // A result of 0 or 1 indicates the drive didn't exist.  Otherwise
  661.                         //   the return result is the type of the drive.  
  662.                         if (driveType > 1)
  663.                             result = TRUE;
  664.                     }
  665.                 }
  666.             }
  667.         }
  668.         FW_CATCH_BEGIN
  669.         FW_CATCH_NO_INSTANCE(FW_XException)
  670.         {
  671.             result = FALSE;
  672.         }
  673.         FW_CATCH_EVERYTHING()
  674.         {
  675.             FW_THROW_SAME();
  676.         }
  677.         FW_CATCH_END
  678.         
  679. #endif
  680.  
  681. #ifdef FW_BUILD_MAC
  682.         short defaultVolumeRefNum = 0;
  683.     
  684.         FW_CString32 newDriveName = driveName;
  685.         Str32 macDriveString;
  686.         
  687.         FW_CharacterPosition pathSeparator = 0;
  688.         OSErr theError = FW_xNoError;
  689.     
  690.         // The last character in the string should be a Mac path separator.
  691.         if (newDriveName.FindCharacter(FW_kPathDelimiter, pathSeparator))
  692.             newDriveName.Truncate(pathSeparator + 1);
  693.         else
  694.             newDriveName += FW_kPathDelimiter;
  695.             
  696.         newDriveName.ExportPascal((FW_PascalChar*)&macDriveString);
  697.     
  698.         // Save the default volume and then attempt to set the default volume to
  699.         //   driveName.  Any errors indicates that the drive doesn't exist.
  700.         theError = ::GetVol(NULL, &defaultVolumeRefNum);
  701.         if (theError == FW_xNoError)
  702.         {
  703.             // Use a non-negative, non-zero number for an invalid drive.
  704.             theError = ::SetVol(macDriveString, 1);
  705.     
  706.             // Restore the default volume.
  707.             ::SetVol(NULL, defaultVolumeRefNum);
  708.     
  709.             if (theError == FW_xNoError)
  710.                 result = TRUE;
  711.         }
  712. #endif
  713.     }
  714.     FW_SOM_CATCH
  715.  
  716.     return (result);
  717. }
  718.  
  719.  
  720. //----------------------------------------------------------------------------------------
  721. //    FW_PrivFileSystem_IsValidFile
  722. //
  723. //  Return TRUE if the file exists.
  724. //----------------------------------------------------------------------------------------
  725.  
  726. FW_Boolean    SL_API         
  727. FW_PrivFileSystem_IsValidFile(Environment* ev, FW_OFileSpecification* theFile)
  728. {
  729.     FW_Boolean result = FALSE;
  730.  
  731.     FW_SOM_TRY
  732.     {
  733. #ifdef FW_BUILD_WIN
  734.         FW_CString fileName;
  735.         FW_TRY
  736.         {
  737.             theFile->GetFullPath(ev, fileName);
  738.     
  739.             char szFileName[_MAX_PATH];
  740.             fileName.ExportCString(szFileName);
  741.     
  742. #ifdef FW_BUILD_WIN16
  743.             FW_FileAccessHandle fileHandle;
  744.             OFSTRUCT Buffer;
  745.     
  746.             fileHandle = ::OpenFile(szFileName, &Buffer, OF_EXIST);
  747.             if (fileHandle != FW_kInvalidAccessHandle)
  748.                 result = TRUE;
  749. #endif
  750.     
  751. #ifdef FW_BUILD_WIN32
  752.             long fileAttributes = 0;
  753.             
  754.             fileAttributes = ::GetFileAttributes(szFileName);
  755.             if (fileAttributes != (-1))
  756.                 if (fileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  757.                     result = TRUE;
  758. #endif
  759.     
  760.         }
  761.         FW_CATCH_BEGIN
  762.         FW_CATCH_EVERYTHING()
  763.         {
  764.             result = FALSE;
  765.         }
  766.         FW_CATCH_END
  767. #endif
  768.  
  769. #ifdef FW_BUILD_MAC
  770.         FSSpec macSpec;
  771.         OSErr theError;
  772.         
  773.         theFile->MacGetFSSpec(ev, &macSpec);
  774.         theError = ::FSMakeFSSpec(macSpec.vRefNum, macSpec.parID, macSpec.name, &macSpec);
  775.         if (theError == noErr)
  776.             result = TRUE;
  777. #endif
  778.     }
  779.     FW_SOM_CATCH
  780.  
  781.     return (result);
  782. }
  783.  
  784.  
  785. #ifdef FW_BUILD_WIN
  786. //----------------------------------------------------------------------------------------
  787. //    FW_PrivFileSystem_WinPathExists
  788. //
  789. //  Return TRUE if the directory exists.
  790. //----------------------------------------------------------------------------------------
  791.  
  792. FW_Boolean    SL_API         
  793. FW_PrivFileSystem_WinPathExists(Environment* ev, FW_HString pathNameRep)
  794. {
  795.     FW_Boolean result = FALSE;
  796.  
  797.     FW_SOM_TRY
  798.     {
  799.         FW_CString pathName(pathNameRep);
  800.  
  801. #ifdef FW_BUILD_WIN16
  802.         FW_CString saveDirectory;
  803.         
  804.         FW_PrivFileSystemParser_PrivGetWorkingDirectory(saveDirectory);
  805.         const FW_Char* namePtr = (const FW_Char*)pathName;
  806.         short validDir = ::chdir((FW_Char*)namePtr);
  807.         if (validDir == 0)
  808.             result = TRUE;
  809.         
  810.         // Restore original directory.
  811.         namePtr = (const FW_Char*)saveDirectory;
  812.         ::chdir((FW_Char*)namePtr);
  813. #endif
  814.  
  815. #ifdef FW_BUILD_WIN32
  816.         long fileAttributes = 0;
  817.         
  818.         char szFileName[_MAX_PATH];
  819.         pathName.ExportCString(szFileName);
  820.     
  821.         fileAttributes = ::GetFileAttributes(szFileName);
  822.         if (fileAttributes != (-1))
  823.             if (fileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  824.                 result = TRUE;
  825. #endif
  826.     }
  827.     FW_SOM_CATCH
  828.  
  829.     return (result);
  830. }
  831. #endif
  832.  
  833.  
  834. #ifdef FW_BUILD_MAC
  835. //----------------------------------------------------------------------------------------
  836. //    FW_PrivFileSystem_MacIsVolumeShared
  837. //
  838. //  Return TRUE if the volume specified by volumeRefNum is currently a shared volume.
  839. //----------------------------------------------------------------------------------------
  840.  
  841. FW_Boolean        SL_API     
  842. FW_PrivFileSystem_MacIsVolumeShared(Environment* ev, short volumeRefNum)
  843. {
  844.     // No try block necessary - Do not throw
  845. FW_UNUSED(ev);
  846.     FW_Boolean result = FALSE;
  847.     HParamBlockRec paramBlock;
  848.     GetVolParmsInfoBuffer volInfoBuffer;
  849.     FW_PlatformError theError = FW_xNoError;
  850.     
  851.     paramBlock.ioParam.ioNamePtr = NULL;
  852.     paramBlock.ioParam.ioVRefNum = volumeRefNum;
  853.     paramBlock.ioParam.ioBuffer = (Ptr)&volInfoBuffer;
  854.     paramBlock.ioParam.ioReqCount = sizeof(volInfoBuffer);
  855.     
  856.     theError = PBHGetVolParmsSync(¶mBlock);
  857.     if (theError == FW_xNoError) 
  858.     {
  859.         if (volInfoBuffer.vMAttrib & FW_kMacVolumeIsSharedBitMask)
  860.             result = TRUE;
  861.     }
  862.         
  863.     return (result);
  864. }
  865. #endif
  866.